package com.mask.im.server.netty;

import io.netty.channel.Channel;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * socket与对应用户的映射
 *
 * @author jian
 */
public class IMChannelCtxMap {

    //保存用户id和channel的映射
    public static Map<String, Channel> userIdChannel = new HashMap<>();
    //保存channelId和channel映射关系
    public static Map<String, Channel> idChannel = new HashMap<>();
    //保存channel和userID映射关系
    public static Map<Channel, String> channelUserId = new HashMap<>();

    public static void setUserIdBindChannel(Channel channel, String userId) {
        userIdChannel.put(userId, channel);
    }

    public static void setIdBindChannel(String id, Channel channel) {
        idChannel.put(id, channel);
    }

    public static void setChannelBindUserId(String userId, Channel channel) {
        channelUserId.put(channel, userId);
    }

    public static void deleteChannelBindUserId(Channel channel) {
        channelUserId.remove(channel);
    }

    public static void deleteBindUserIdChannel(String userId) {
        userIdChannel.remove(userId);
    }

    public static void deleteBindIdChannel(String id) {
        idChannel.remove(id);
    }

    public static void setUserIdAndIdBindChannel(String id, Channel channel) {
        setUserIdBindChannel(channel, id);
        setIdBindChannel(id, channel);
    }

    public static void deleteBindUserIdAndIdChannel(String id) {
        deleteBindIdChannel(id);
        deleteBindUserIdChannel(id);
    }

    public static Channel getUserIdChannel(String userId) {
        return userIdChannel.get(userId);
    }

    public static String getChannelUserId(Channel channel) {
        return channelUserId.get(channel);
    }

    public static List<Channel> getAllOnlineUser() {
        return new ArrayList<>(channelUserId.keySet());
    }

}
